// ActionScript file
import org.un.cava.birdeye.ravis.components.renderers.edgeLabels.ShiningLineLabelRender;
import org.un.cava.birdeye.ravis.graphLayout.data.Graph;
import org.un.cava.birdeye.ravis.graphLayout.data.IEdge;
import org.un.cava.birdeye.ravis.graphLayout.data.IGraph;
import org.un.cava.birdeye.ravis.graphLayout.data.INode;
import org.un.cava.birdeye.ravis.graphLayout.visual.IVisualEdge;
import org.un.cava.birdeye.ravis.utils.events.GraphChangeEvent;
							
			/* Init function, initiates the HTTP
			 * request. All further initalisation is done
			 * in the event handler for the HTTP Service */
			
			private var topNetworkName:String = "top";
			private var updateXMLFile:String = "data/update.xml";
			private var updateInterval:uint = 5000;
			private var networks:Dictionary=new Dictionary();
			private var netXMLs:Dictionary=new Dictionary();
			private var track:Array = new Array();
			private var currentIndex:uint=0;
			private var timer:Timer = new Timer(updateInterval,0);
			private var updateXML:XML;
			private var myLoader:URLLoader;
			
			
		 
			private function init():void {
				initData();
			}
			
			/**
			 * event handler for HTTPService, does all the remaining required
			 * initialisation */
			private function initData():void {
				
				/* init a graph object with the XML data  <Network id="top">*/
				for each(var networkName:String in xmldata.Network.@id){
					networks[networkName]=networkName;
				}

				//currentNetworkName=topNetworkName;
				var topNetworkXML:XML=XML(xmldata.Network.(@id==topNetworkName));
				track.push(topNetworkName);
				netXMLs[topNetworkName]=topNetworkXML;
				vgraph.graph = new Graph(topNetworkName,false,topNetworkXML);
				vgraph.addEventListener(GraphChangeEvent.GRAPH_CHANGED,handlingGraphChange);

				/* assign start root */
				vgraph.currentRootVNode = vgraph.graph.nodeByStringId("0").vnode;
				vgraph.scrollBackgroundInDrag=false;
	
				/* provide the control components with some initial settings */
				myLayoutSelector.selectedItem = "ConcentricRadial";
				myLayoutSelector.changeLayouter();
	
				myNodeRendererSelector.selectedItem = "SubNetwork";
				myNodeRendererSelector.changeNodeRenderer();
				
				myEdgeRendererSelector.selectedItem = "Default";
				myEdgeRendererSelector.changeEdgeRenderer();
			
				myELCs.elselector.selectedItem = "IPTransfering";
				myELCs.elselector.changeEdgeLabelRenderer();
				
				myDegreeOS.updateMaxVisDist();
				
				/* the following kicks it off .... */
				vgraph.draw();
				timer.addEventListener(TimerEvent.TIMER,updateTransferFrequency);
				timer.start();
			}
			
			private function handlingGraphChange(eventObj:GraphChangeEvent):void{
				
				var networkName:String = eventObj.networkName;
				if(checkNetworkExist(networkName)){
					var networkXML:XML=XML(xmldata.Network.(@id==networkName));
					netXMLs[networkName]=networkXML;
					vgraph.graphRefresh(networkName,networkXML,false);
					track.push(networkName);
					currentIndex++;
					vgraph.setStyle("backgroundImage","assets/imgs/jilin.JPG");
					
				}
			}
			private function checkNetworkExist(networkName:String):Boolean{
				if(networks[networkName]==null)
					return false;
				else
					return true;
			}
			private function preVgraph(eventObj:Event):void{
				if(currentIndex==0)
					return;
				else{
					currentIndex--;
				
				var networkName:String = track[currentIndex];
					var networkXML:XML=netXMLs[networkName];
					vgraph.graphRefresh(networkName,networkXML,false);
					if(currentIndex==0){
						vgraph.setStyle("backgroundImage","assets/imgs/map2.gif");
					}
				}
				}
		
			private function postVgraph(eventObje:Event):void{
					if(currentIndex==track.length-1)
					return;
				else{  
					currentIndex++;
				
				var networkName:String = track[currentIndex];
					var networkXML:XML=netXMLs[networkName];
					vgraph.graphRefresh(networkName,networkXML,false);
					
					
				}
				
			}
			private function updateTransferFrequency(eventObj:TimerEvent):void{
						
				var XML_URL:String = updateXMLFile;
				var myXMLURL:URLRequest = new URLRequest(XML_URL);
				myLoader = new URLLoader(myXMLURL);
				myLoader.addEventListener("complete", xmlLoaded);
			}
			private function xmlLoaded(event:Event):void
			{	var networkName:String;
				var fromNodeID:String;
				var toNodeID:String;
				var fromNode:INode;
				var toNode:INode;
				var updadeFrequency:String;
				var toTransfer:String;
				var graph:IGraph;
				var iedge:IEdge;
				var vEdge:IVisualEdge;
				var shiningLine:ShiningLineLabelRender;
				updateXML = XML(myLoader.data);
				for each(var network:XML in updateXML.Network){
					networkName=network.@id;
					if(vgraph.graph.id==networkName){
						graph=vgraph.graph;
						for each(var edge:XML in network.Edge ){
							fromNodeID=edge.@fromID;
							toNodeID=edge.@toID;
							fromNode=graph.nodeByStringId(fromNodeID);
							toNode=graph.nodeByStringId(toNodeID);
							updadeFrequency=edge.@frequency;
							toTransfer=edge.@transferring;
							iedge=graph.getEdge(fromNode,toNode);
							if(iedge!=null){
							vEdge=iedge.vedge;		
							//xmldata.Network.(@id==networkName).Edge.(@fromID==fromNodeID&&@toID==toNodeID).@frequency=updadeFrequency;
							xmldata.Network.(@id==networkName).Edge.(@fromID==fromNodeID&&@toID==toNodeID).@transferring=toTransfer;
							if(vEdge.labelView is ShiningLineLabelRender){
								shiningLine = ShiningLineLabelRender(vEdge.labelView);
								shiningLine.transferFrequency=uint(updadeFrequency);			
							}
							}else{
								var tem:String="<Edge fromID=\""+fromNodeID+"\" toID=\""+toNodeID+
								"\" transferring=\""+toTransfer+"\" frequency=\""+updadeFrequency+"\"/>";
								var newEdgeXML:XML=new XML(tem);
								(xmldata.Network.(@id==networkName) as XML).appendChild(newEdgeXML);
								vgraph.graph.link(fromNode,toNode);
								trace("#####################graph link successed!");
								vgraph.linkNodes(fromNode.vnode,toNode.vnode);
							}
						}
					}
				}
				
			}